//
//  FYStatusTableViewCell.swift
//  Mircore
//
//  Created by apple on 16/8/12.
//  Copyright © 2016年 飞羽. All rights reserved.
//

import UIKit

import SnapKit


//子控件之间的间距
let  StatusTableViewCellMargin: CGFloat  = 10

//微博首页字体大小

let  StatusFontSize: CGFloat = 14

//自定义首页微博数据的cell
class FYStatusTableViewCell: UITableViewCell {
    
    //toolbar底部约束记录
    
    var  toolBarTopConstraint: Constraint?
    
    //处理微博数据的ViewModel
    
    var  statusViewModel: FYStatusViewModel? {
        
        didSet {
            
            //设置数据
            originView.statusViewModel  = statusViewModel
            
            toolBar.statusViewModel  =  statusViewModel
            
            
            
            //卸载上一次约束
            
            toolBarTopConstraint?.uninstall()
            
            //判断是否有转发微博对象
            
            if  statusViewModel?.status?.retweeted_status  !=  nil  {
                
                //显示转发微博视图
                
                retweetView.hidden  = false
                
                //更新约束
                
                toolBar.snp_updateConstraints(closure: { (make) in
                    
                    self.toolBarTopConstraint  = make.top.equalTo(retweetView.snp_bottom).constraint
                    
                })
                
                //绑发转发微博数据
                
                retweetView.statusViewModel  = statusViewModel
            }else  {
                
                
                //隐藏转发微博视图
                
                retweetView.hidden  = true
                
                
                //更新约束
                
                toolBar.snp_updateConstraints(closure: { (make) in
                    
                    self.toolBarTopConstraint  = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
        
    }
    
    //MARK:-懒加载控件
    
    //原创微博视图
    private lazy  var originView: FYStatusOriginView = {
        
        let view = FYStatusOriginView()
        
         return view
        
    }()
    
    //转发微博的视图
    
    private  lazy var retweetView: FYStatusRetweetView = {
        
        let  view  = FYStatusRetweetView()
        
        return view
        
    }()
    
    //toolbar
    
    private lazy  var  toolBar:  FYStatusToolBar = FYStatusToolBar()
    
    override  init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required  init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has  not been  implemented")
        
        
    }
    
    //添加控件  设置约束
    
    
    private  func  setupUI() {
        
        //修改contentView的背景色
        
        contentView.backgroundColor  = UIColor(white: 0.95, alpha: 1)
        
        contentView.addSubview(originView)
        
        contentView.addSubview(retweetView)
        
        contentView.addSubview(toolBar)
        
        
        //设置约束
        
        originView.snp_makeConstraints { (make) in
            
            make.top.equalTo(contentView)
            
            make.leading.equalTo(contentView)
            
            make.trailing.equalTo(contentView)
        }
        
        
        retweetView.snp_makeConstraints { (make) in
            
            make.top.equalTo(originView.snp_bottom)
            
            make.leading.equalTo(originView)
            
            make.trailing.equalTo(originView)
        }
        
        toolBar.snp_makeConstraints { (make) in
            
            self.toolBarTopConstraint =  make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.leading.equalTo(contentView)
            
            make.trailing.equalTo(contentView)
            
            make.height.equalTo(35)
        }
        
        //关键约束
        
        contentView.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(toolBar)
            
            make.top.equalTo(self)
            
            make.leading.equalTo(self)
            
            make.trailing.equalTo(self)
        }
        
    }
}
